package com.rubiksillusions;

public class PositionThreeEdgesOfSecondLayerPatternCreator {
	public static void main(String[] args) {
		String finalStrings[] = {
			"................r.....g..........................rg...",
			"............g.........gr.........................r...."
		};
		
		String initStrings[] = {
//			"......................g..g....................r..r....",
//			"......................g..r....................g..r....",
//			
//			"......................g.....r....................r..g.",
//			"......................g.....g....................r..r.",
			
			"......................g...........r......g.......r....",
			"......................g...........g......r.......r....",
			
			"...................r..g................g.........r....",
			"...................g..g................r.........r....",
			
			"..........r...........g....................g.....r....",
			"..........g...........g....................r.....r....",
			
			"..............r.......g.........g................r....",
			"..............g.......g.........r................r....",
			
			"............r.........gg.........................r....",
			
			"................g.....g..........................rr..."
		};
		
		String[] masks = {
//			"ww.w.wwww.........g..g.....b..b..b..ooo.........r..r..", // Cubelet 6, 0, 2 (1, 3, 5, 7)
//			"ww.w.www..........g..g........b..b..ooo.........r.....", // Cubelet 6, 0 (1, 3, 5, 7)
//			".w.w.wwww............g.....b..b..b...oo.........r..r..", // Cubelet 2, 0 (1, 3, 5, 7)
//			"ww.w.w.ww.........g..g.....b..b.....bo..........r..r..", // Cubelet 6, 2 (1, 3, 5, 7)
//			"ww.w.w.w..........g..g........b.....oo..........r.....", // Cubelet 6 (1, 3, 5, 7)
//			".w.w.www.............g........b..b...oo.........r.....", // Cubelet 0 (1, 3, 5, 7)
//			".w.w.w.ww............g.....b..b......o..........r..r..", // Cubelet 2 (1, 3, 5, 7)
			"ww.wwwwww.........g..g.....b..b..b..ooo.........r..r.." // Nessun Cubelet (1, 3, 5, 7)
		};
		
		for (int k = 0; k < masks.length; k++) {
			for (int j = 0; j < initStrings.length; j++) {
				String output = getJavaForPattern(
					"",
					initStrings[j],
					finalStrings,
					masks[k]
				);
				if (output != null && !output.equals("")) System.out.println(output);
			}
			System.out.println("\n\n");
		}
	}
	
	private static String getJavaForPattern(String name, String initString, String[] finalStrings, String mask) {
		String javaString = "";
		String moves = getMovesForPattern(initString, finalStrings, mask);
		if (moves == null) return "";
		if (moves.equals("")) return "";
		javaString += "if (!executed) executed = rubiksCube.executeStatusMovesList(" +
			"\"" + initString + "\", " + "\"" +
			moves + "\", " + "true);";
		return javaString;
	}
	
	private static String getMovesForPattern(String initString, String[] finalStrings, String mask) {
		int MAX_MOVES = 3;
		String tempInitString = "";
		String[] tempFinalStrings = new String[finalStrings.length];
		for (int i = 0; i < tempFinalStrings.length; i++) {
			tempFinalStrings[i] = "";
		}
		
		
		for (int i = 0; i < mask.length(); i++) {
			if (mask.charAt(i) != '.') {
				if (initString.charAt(i) != '.') return null;
				tempInitString += mask.charAt(i);
				for (int j = 0; j < finalStrings.length; j++) {
					tempFinalStrings[j] += mask.charAt(i);
				}
				
			} else {
				tempInitString += initString.charAt(i);
				for (int j = 0; j < finalStrings.length; j++) {
					tempFinalStrings[j] += finalStrings[j].charAt(i);
				}
			}
		}
		int i = 0, movesCount = 0;
		while (movesCount <= MAX_MOVES) {
			int temp = i;
			String out = "";
			out += getMoveByNumber(temp % 18) + " ";
			while (temp / 18 != 0) {
				temp = temp / 18;
				out += getMoveByNumber(temp % 18) + " ";
			}
			
			String[] splitted = out.split(" ");
			movesCount = splitted.length;
			if (movesCount > MAX_MOVES) break;
			
			RubiksCube initCube = new RubiksCube(tempInitString);
			initCube.executeMovesList(out, true);
			
			String exCube = initCube.getFaceletsString();
			//if (i % 100 == 0) System.out.println(i);
			//System.out.println(exCube + " " + out);
			i++;
			
			for (int j = 0; j < tempFinalStrings.length; j++) {
				//System.out.println("    " + tempFinalStrings[j]);
				if (exCube.equals(tempFinalStrings[j])) {
					return out;
				}
			}
		}
		return "";
	}
	
	private static String getMoveByNumber(int number) {
		switch (number) {
			case 0: return "R";
			case 1: return "L";
			case 2: return "U";
			case 3: return "D";
			case 4: return "F";
			case 5: return "B";
			case 6: return "R'";
			case 7: return "L'";
			case 8: return "U'";
			case 9: return "D'";
			case 10: return "F'";
			case 11: return "B'";
			case 12: return "R2";
			case 13: return "L2";
			case 14: return "U2";
			case 15: return "D2";
			case 16: return "F2";
			case 17: return "B2";
		}
		return "";
	}
}
